// pages/user/address/editor/editor.js
var openid = wx.getStorageSync("openId");
const token = wx.getStorageSync('token');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: true,
    username: '',//收货人
    mobile: '',//联系电话
    areaList: [],
    area: '',
    show: false,
    address: '',//收货所在地区
    addressDetails: '',//详细地址
    id:'',//地址id
  },

  //输入框 username的执行逻辑
  handleName: function (e) {
    this.setData({
      username: e.detail
    })
  },

  //输入框 mobile的执行逻辑
  handleMobile: function (e) {
    this.setData({
      mobile: e.detail
    })
  },

  //输入框 address的执行逻辑
  handleAddress: function (e) {
    this.setData({
      address: e.detail
    })
  },

  //输入框 addressDetails的执行逻辑
  handleAddressDetails: function (e) {
    this.setData({
      addressDetails: e.detail
    })
  },

  onChange({ detail }) {
    // 需要手动对 checked 状态进行更新
    this.setData({ checked: detail });
  },

  //聚焦所在地区文本框，弹出选择框
  showPopup() {
    this.setData({ show: true })
  },

  cancelChoose() {
    this.setData({ show: false });
  },

  chooseAddress(e) {
    let ads = e.detail.values;
    let province = ads[0].name;
    let city = ads[1].name;
    let district = ads[2].name;
    this.setData({ address: province + ' ' + city + ' ' + district });
    this.setData({ show: false });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    //获取地址对应的id值
    let that = this;
    console.log(e.id);
    let id = e.id;
    that.setData({
      id:id,
    })
    wx.setNavigationBarTitle({
      title: '编辑收获地址'
    })

    const db = wx.cloud.database()
    db.collection('area').get().then(res => {
      // res.data 是一个包含集合中有权限访问的所有记录的数据，不超过 20 条
      if (res.data && res.data.length > 0) {
        this.setData({
          areaList: res.data[0],
        })
      }
    })
      .catch((err) => {
        console.log(err)
      })

    //发送请求给后端接口，根据id获取地址的各个字段，然后渲染到文本框中
      wx.request({
        header:{
          "Authorization": token
        },
        url: 'http://localhost:8080/addresss/selectAddressInfoById/' + id,
        success(res) {
          console.log(res.data);
          console.log(res.data.data.detailAddress);
          let address = res.data.data.detailAddress;
          let arr = address.split("-");
          if(res.data.data.isDefault == 0) {
            that.setData({
              username:res.data.data.receiver,
              mobile:res.data.data.telephone,
              checked:false,
              address:arr[0],
              addressDetails:arr[1]
            })
          } else {
            that.setData({
              username:res.data.data.receiver,
              mobile:res.data.data.telephone,
              checked:true,
              address:arr[0],
              addressDetails:arr[1]
            })
          }
        }
      })
  },

  //点击保存按钮，更新数据库地址信息
  updateAddress() {
    let that = this
    let id = that.data.id
    let address = that.data.address + '-' + that.data.addressDetails
    let check;
    that.data.checked ? check = 1 : check = 0
    wx.request({
      header:{
        "Authorization": token
      },
      url: 'http://localhost:8080/addresss/updateAddress/' + id,
      data: {
        openId:openid,
        receiver:that.data.username,
        telephone:that.data.mobile,
        detailAddress:address,
        isDefault:check
      },
      success(res) {
        if(res.data.code == 200) {
          wx.showToast({
            title: '修改成功',
          })
          wx.navigateTo({
            url: '/pages/user/address/myaddress/index',
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})